package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.TransferClassesPriceDao;
import com.example.domain.TransferClassesPrice;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.TransferClassesPriceService;
import com.example.tool.DateTool;

@Service
public class TransferClassesPriceServiceImpl implements TransferClassesPriceService {

    @Autowired
    TransferClassesPriceDao transferClassesPriceDao;

    /**
     * @describe 添加转班费信息
     */
    @Override
    public Result addTransferClassesPrice(TransferClassesPrice transferClassesPrice) {
        if (transferClassesPrice != null) {
            // 获取系统当前时间作为记录创建时间
            transferClassesPrice.setRecordTime(DateTool.getTimestamp());
        }
        // 调用JPA保存实体方法
        TransferClassesPrice transferClassesPrice1 = transferClassesPriceDao.save(transferClassesPrice);

        if (transferClassesPrice1 != null) {
            return ResultFactory.buildSuccessResult(transferClassesPrice1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新转班费信息
     */
    @Override
    public Result updateTransferClassesPrice(TransferClassesPrice transferClassesPrice) {
        if (transferClassesPrice != null) {
            // 获取系统当前时间作为记录更新时间
            transferClassesPrice.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        TransferClassesPrice transferClassesPrice1 = transferClassesPriceDao.saveAndFlush(transferClassesPrice);

        if (transferClassesPrice1 != null) {
            return ResultFactory.buildSuccessResult(transferClassesPrice1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据转班费编号删除转班费信息
     */
    @Override
    public Result deleteTransferClassesPriceById(String transferPriceId) {
        int row = transferClassesPriceDao.deleteByTransferPriceId(Integer.parseInt(transferPriceId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有转班费信息
     */
    @Override
    public Result queryAllTransferClassesPrice() {
        List<TransferClassesPrice> list = transferClassesPriceDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询转班费信息
     */
    @Override
    public Result queryTransferClassesPriceById(String transferPriceId) {
        List<TransferClassesPrice> list = transferClassesPriceDao.findByTransferPriceId(Integer.parseInt(transferPriceId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加转班费信息
     */
    @Override
    public Result addTransferClassesPriceByExcel(List<TransferClassesPrice> list) {
        List<TransferClassesPrice> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = transferClassesPriceDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据转班费编号批量删除转班费信息
     */
    @Override
    public Result deleteTransferClassesPriceByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = transferClassesPriceDao.deleteByTransferPriceIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 转班费信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<Map<String, Object>> list = null;

        if (info[0].equals("originalClass")) {

            list = transferClassesPriceDao.findByOriginalClassLike(info[1]);

        } else if (info[0].equals("nowClass")) {

            list = transferClassesPriceDao.findByNowClassLike(info[1]);

        } else if (info[0].equals("originalClassMoney")) {

            list = transferClassesPriceDao.findByOriginalClassMoneyLike(info[1]);

        } else if (info[0].equals("nowClassMoney")) {

            list = transferClassesPriceDao.findByNowClassMoneyLike(info[1]);

        } else if (info[0].equals("priceSpread")) {

            list = transferClassesPriceDao.findByPriceSpreadLike(info[1]);

        }

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result queryAllStudentAndTransferClassPriceInfo() {
        // TODO 查询所有学生转班费用信息
        List<Map<String, Object>> list = transferClassesPriceDao.queryAllStudentAndTransferClassPriceInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result queryAllStudentAndTransferClassInfo() {
        // TODO 查询所有学生转班且状态已批阅信息
        List<Map<String, Object>> list = transferClassesPriceDao.queryAllStudentAndTransferClassInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    @Override
    public Result selectTransferTuitionByUserId(Integer userId) {
        // TODO 根据学生ID查询转班费用信息
        List<Map<String, Object>> list = transferClassesPriceDao.selectTransferTuitionByUserId(userId);
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
